#include "bmpinterface.h"

BitmapInterface::BitmapInterface(Message* msg, Parameters* par) {
	
	// intialize variables
	message = msg;
	params  = par;
	has_errors = 0;
	open_read=-1;
}

void BitmapInterface::openForRead(const char* filename) {

	// 0. initializations
	open_read  = 1;
	has_errors = 0;
	
	// 1. announce opening bitmap 
	if (params->getVerbose()) {
		message->printMessage("Opening input BMP file: ");
		message->printMessage(filename);
		message->printMessage(".\n");
	}
	
	// 2. create a new bitmap class
	workFile.ReadFromFile( params->getInputFilename() );
	if ( (workFile.TellHeight() == 1) && (workFile.TellWidth() == 1) ) {
		/* 
		 * this indicates an error in EasyBMP. It has probably printed the error
		 * and all we need to do here is set has_errors=true:
		 */
		has_errors=1;
		return;
	}
	
	// 3. set sizes and other parameters
	real_height = local_height = workFile.TellHeight();
	real_width  = local_width  = workFile.TellWidth();		
}


void BitmapInterface::openForWrite(const char* filename) {
	// 0. initializations
	open_read  = 0;
	has_errors = 0;

	// TODO: Make this support color
	workFile.SetBitDepth(8);
	CreateGrayscaleColorTable(workFile);
	
	// Ok, now set the size and call writeFile to submit
}

void BitmapInterface::writeFile() {
	workFile.WriteToFile(params->getOutputFilename());
}

void BitmapInterface::writeFile(char* name) {
	workFile.WriteToFile(name);
}

int BitmapInterface::hasErrors() {
	return has_errors;
}

int BitmapInterface::getRealHeight() {
	return real_height;
}

int BitmapInterface::getRealWidth() {
	return real_width;
}

int BitmapInterface::getLocalHeight() {
	return local_height;	
}

int BitmapInterface::getLocalWidth() {
	return local_width;
}

void BitmapInterface::setRealSize(int height, int width) {
	real_height=height;
	real_width=width;
	workFile.SetSize(width, height);
}

void BitmapInterface::setLocalSize (int height, int width) {
	local_height=height;
	local_width=width;
}


double BitmapInterface::getPixel(int x, int y) {
	if ((x<real_width)&&(y<real_height)) {
		double red = (double)workFile.GetPixel(x,y).Red;
		double blue = (double)workFile.GetPixel(x,y).Blue;
		double green = (double)workFile.GetPixel(x,y).Green;
		return (double) (red+blue+green)/(255*3);
	}
	else 
		return OUTSIDE_COLOR;
}

double BitmapInterface::getPixelR(int x, int y){
	if ((x<real_width)&&(y<real_height)) {
		return ((double)workFile.GetPixel(x,y).Red)/255;
	}
	else 
		return OUTSIDE_COLOR;
}

double BitmapInterface::getPixelG(int x, int y){
	if ((x<real_width)&&(y<real_height)) {
		return ((double)workFile.GetPixel(x,y).Red)/255;
	}
	else 
		return OUTSIDE_COLOR;
}

double BitmapInterface::getPixelB(int x, int y){
	if ((x<real_width)&&(y<real_height)) {
		return ((double)workFile.GetPixel(x,y).Red)/255;
	}
	else 
		return OUTSIDE_COLOR;
}


void BitmapInterface::setPixel(int x, int y, double value) {
	RGBApixel pix;
	
	if ((x<real_width)&&(y<real_height)) {
		pix.Red=(ebmpBYTE)(value*255);
		pix.Green=(ebmpBYTE)(value*255);
		pix.Blue=(ebmpBYTE)(value*255);
		workFile.SetPixel(x,y,pix);
	}
}

void BitmapInterface::setPixelRGB(int x, int y, double red, double green, double blue) {
	if ((x<real_width)&&(y<real_height)) {
		RGBApixel pix;
		pix.Red=(ebmpBYTE)(red*255);
		pix.Green=(ebmpBYTE)(green*255);
		pix.Blue=(ebmpBYTE)(blue*255);
		workFile.SetPixel(x,y,pix);
	}
}
